import { defineStore } from 'pinia'
import { ref } from 'vue'
import { favorite, like, cancelFavorite, cancelLike } from '@/services/article'
import { Toast } from 'vant'
import 'vant/es/toast/style'
/**
 * 文章store：存储登录用户相关数据
 */

export const useArticleStore = defineStore(
  'gh-article',
  () => {
    //全局变量：存放分类数组的属性
    //点赞
    const likes = ref(0)
    //评论数
    const comments = ref(0)

    //获取文章的点赞数和评论数
    const setArticleStory = async (likeNum, commentNum) => {
      likes.value = likeNum
      comments.value = commentNum
    }

    //点赞功能
    const incrementLikes = async (article_id, user_id) => {
      const res = await like(article_id, user_id)
        .then((response) => {
          return response
        })
        .catch((error) => {
          return error
        })
      if (res.status !== 201) {
        if (res.response.status == 402) {
          const toastMessage = res.response.data.error
          Toast.fail(toastMessage)
          return
        }
        if (res.response.status == 404) {
          const toastMessage = res.response.data.error
          Toast.fail(toastMessage)
          return
        }
      }
      Toast('点赞成功！')
      // console.log(res)
      likes.value++
    }
    //取消点赞功能
    const removeLike = async (article_id, user_id) => {
      const res = await cancelLike(article_id, user_id)
        .then((response) => {
          return response
        })
        .catch((error) => {
          return error
        })
      if (res.status !== 201) {
        if (res.response.status == 402) {
          const toastMessage = res.response.data.error
          Toast.fail(toastMessage)
          return
        }
        if (res.response.status == 404) {
          const toastMessage = res.response.data.error
          Toast.fail(toastMessage)
          return
        }
      }
      Toast('取消点赞！')
      likes.value--
    }
    //收藏功能
    const incrementFavorites = async (article_id, user_id) => {
      const res = await favorite(article_id, user_id)
        .then((response) => {
          return response
        })
        .catch((error) => {
          return error
        })
      if (res.status !== 201) {
        if (res.response.status == 402) {
          const toastMessage = res.response.data.error
          Toast.fail(toastMessage)
          return
        }
        if (res.response.status == 404) {
          const toastMessage = res.response.data.error
          Toast.fail(toastMessage)
          return
        }
      }
      // console.log(res)
      Toast('收藏成功！')
      // favorites.value++
      // console.log(favorites.value)
    }
    //取消收藏功能
    const removeFavorite = async (article_id, user_id) => {
      const res = await cancelFavorite(article_id, user_id)
        .then((response) => {
          return response
        })
        .catch((error) => {
          return error
        })
      if (res.status !== 201) {
        if (res.response.status == 402) {
          const toastMessage = res.response.data.error
          Toast.fail(toastMessage)
          return
        }
        if (res.response.status == 404) {
          const toastMessage = res.response.data.error
          Toast.fail(toastMessage)
          return
        }
      }
      Toast('取消收藏！')
      // favorite.value--
    }

    return {
      likes,
      comments,
      incrementLikes,
      incrementFavorites,
      removeFavorite,
      removeLike,
      setArticleStory
    }
  },
  { persist: true } //开启数据持久化
)
